import MyWebSocket from "../api/ws.min.js";
import store from "../store";
export default function wsHook(){
    let ws = new MyWebSocket();
    let isCef = true;
    let baseInfo = {
        //ip: '127.0.0.1',
        //ip: '192.168.72.1',
        ip: '192.168.43.154',
        port: '8888',
        path: "hzdtpxzx",
    };
    try {
        ws.onMessage = function (msg) {
            if(msg.indexOf('code') === -1){
                return
            }
            //console.log('msg',msg)
            try {
                //console.log(msg)
                let temp =JSON.parse(msg);
                if(temp.hasOwnProperty("relayState")){
                    store.commit('global/evaluation',temp.relayState)
                }else if(temp.hasOwnProperty("faultPointJArr")){
                    if(temp.faultPointJArr.length !== 0){
                        //console.log( Object.keys(temp.faultPointJArr)[0][1])
                        store.commit('global/evaluationfaultPointJArr',
                            {id:Object.keys(temp.faultPointJArr)[0][1],line :temp.faultPointJArr}
                        )
                    }
                }
            }catch (err){
                console.log(err);
            }
        };
        ws.onError = function (error) {
            console.log(error);
        };
        ws.onConnectionFail = function () {
            alert('连接失败！');
        };
        Bridge.getUrlInfo()
            .then((result) => {
                console.log(result);
                baseInfo = JSON.parse(result);
                ws.open(baseInfo);
            })
            .catch(() => {});
    } catch (error) {
        isCef = false;
        console.log(error,'Is no run cef!');
    }
    finally {
        if (!isCef) {
            ws.open(baseInfo);
        }
    }
    return ws;
}
